// pages/address/address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dizhi: [],
    currentIndex:0
  },
  //获取收货地址
  getAdress() {
    let that = this
    let dizhi = this.data.dizhi
    if (dizhi.length <= 4) {
      wx.chooseAddress({
        success: (res) => {
          let name = res.userName
          let telephone = res.telNumber
          let address = res.provinceName + res.cityName + res.countyName + res.detailInfo
          dizhi.push({
            name,
            telephone,
            address
          })
          that.setData({
            name,
            telephone,
            address,
            dizhi
          })
          wx.setStorageSync('dizhi', dizhi)
        },
      })
    } else {
      wx.showToast({
        icon: 'error',
        title: '数量最多5个',
      })
    }
  },
  //选择收货地址
  choose(e){
    let index = e.currentTarget.dataset.index
    console.log("选择的地址下标",index);
    this.setData({
      currentIndex:index
    })
  },
  //删除收货地址
  del(e) {
    let that = this;
    let dizhi = this.data.dizhi
    wx.showModal({
      title: '确认删除嘛？',
      showCancel: true,
      cancelText: '取消',
      confirmText: '确认',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确认')
          let index = e.currentTarget.dataset.index
          dizhi.splice(index, 1)
          that.setData({
            dizhi
          })
          wx.setStorageSync('dizhi', dizhi)
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let dizhi = wx.getStorageSync('dizhi') || []
    console.log(dizhi);
    this.setData({
      dizhi: dizhi
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})